package com.ihd.ihardware.view.tzc.me.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.RulerView;
import com.ihd.ihardware.databinding.ActivityTargetWeightBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.TargetViewModel;

import java.math.BigDecimal;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class TargetWeightActivity extends BaseActivity<ActivityTargetWeightBinding, TargetViewModel> {

    private UserInfoRes.DataBean ud;
    private float weightDiff;
    private float per;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("目标体重");
        binding.mtitlebar.setLeftBack(this);

        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);

        binding.target0.rulerHeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                binding.target0.currWeightTV.setText(Float.toString(value));
                if (Float.parseFloat(ud.getWeight()) < value) {
                    weightDiff = value - Float.parseFloat(ud.getWeight());
                    binding.target0.needHintTV.setText("需增重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
                } else {
                    weightDiff = Float.parseFloat(ud.getWeight()) - value;
                    binding.target0.needHintTV.setText("需减重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
                }
            }
        });
        if (TextUtils.isEmpty(ud.getDreamWeight())) {
            if (Float.parseFloat(ud.getWeight()) < 50f) {
                weightDiff = 50f - Float.parseFloat(ud.getWeight());
                binding.target0.needHintTV.setText("需增重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
            } else {
                weightDiff = Float.parseFloat(ud.getWeight()) - 50f;
                binding.target0.needHintTV.setText("需减重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
            }
            binding.target0.rulerHeight.setValue(50.0f, 30.0f, 150.0f, 0.1f);
        } else {
            if (Float.parseFloat(ud.getWeight()) < Float.parseFloat(ud.getDreamWeight())) {
                weightDiff = Float.parseFloat(ud.getDreamWeight()) - Float.parseFloat(ud.getWeight());
                binding.target0.needHintTV.setText("需增重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
            } else {
                weightDiff = Float.parseFloat(ud.getWeight()) - Float.parseFloat(ud.getDreamWeight());
                binding.target0.needHintTV.setText("需减重" + new BigDecimal(weightDiff).setScale(1, 4).floatValue() + "公斤");
            }
            binding.target0.currWeightTV.setText(ud.getDreamWeight());
            binding.target0.rulerHeight.setValue(Float.parseFloat(ud.getDreamWeight()), 30.0f, 150.0f, 0.1f);
        }

        binding.target1.rulerPeriod.showPeriodText(true);
        binding.target1.rulerPeriod.setValue(TextUtils.isEmpty(ud.getDreamWeightPeriod()) ? 11.0f : Float.parseFloat(ud.getDreamWeightPeriod()), 1.0f, 52.0f, 1f);
        binding.target1.periodTV.setText(TextUtils.isEmpty(ud.getDreamWeightPeriod()) ? "11" : ud.getDreamWeightPeriod());
        binding.target1.rulerPeriod.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                binding.target1.periodTV.setText(String.valueOf((int) value));
                per = weightDiff / value;
                if (binding.target0.needHintTV.getText().toString().contains("减重"))
                    binding.target1.periodWTV.setText("每周减重" + new BigDecimal(per).setScale(1, 4).floatValue() + "公斤");
                else
                    binding.target1.periodWTV.setText("每周增重" + new BigDecimal(per).setScale(1, 4).floatValue() + "公斤");
            }
        });

        binding.target0.nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.target0.t0.setVisibility(View.GONE);
                binding.target1.t1.setVisibility(View.VISIBLE);
                per = weightDiff / 11.0f;
                if (binding.target0.needHintTV.getText().toString().contains("减重"))
                    binding.target1.periodWTV.setText("每周减重" + new BigDecimal(per).setScale(1, 4).floatValue() + "公斤");
                else
                    binding.target1.periodWTV.setText("每周增重" + new BigDecimal(per).setScale(1, 4).floatValue() + "公斤");
            }
        });

        binding.target1.nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.target1.t1.setVisibility(View.GONE);
                binding.target2.t2.setVisibility(View.VISIBLE);
                binding.target2.weightDiffTV.setText(new BigDecimal(weightDiff).setScale(1, 4).toString());
                binding.target2.targetWTV.setText(binding.target0.currWeightTV.getText().toString());
                binding.target2.periodTV.setText(binding.target1.periodTV.getText().toString());
                binding.target2.perTV.setText(new BigDecimal(per).setScale(1, 4).toString());
            }
        });

        binding.target2.nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.dream(binding.target0.currWeightTV.getText().toString(), binding.target1.periodTV.getText().toString(), TextUtils.isEmpty(ud.getDreamStepNumber()) ? "0" : ud.getDreamStepNumber());
            }
        });

        binding.target2.resetTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(TargetWeightActivity.this, TargetWeightActivity.class);
                startActivity(i);
                finish();
            }
        });

        setRx(AppConstans.DREAM, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(TargetWeightActivity.this);
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.dismiss(TargetWeightActivity.this);
                ud.setDreamWeight(binding.target0.currWeightTV.getText().toString());
                ud.setDreamWeightPeriod(binding.target1.periodTV.getText().toString());
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(ud, UserInfoRes.DataBean.class));
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(TargetWeightActivity.this, error.getMessage());
                DialogUtils.dismiss(TargetWeightActivity.this);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_target_weight;
    }

    @Override
    protected Class getViewModelClass() {
        return TargetViewModel.class;
    }
}
